package com.societyGames.codeAlong.state.codeExercise.model.link
{
import com.societyGames.codeAlong.state.codeExercise.model.ClassData;
import com.societyGames.codeAlong.state.codeExercise.model.FunctionData;
import com.societyGames.codeAlong.state.codeExercise.model.FunctionParameterData;
import com.societyGames.codeAlong.state.codeExercise.model.LibraryData;

public class LinkHash
{
  private var _hash:Object = {}; //string to string or ILinkAction

  public function LinkHash()
  {
  }

  public function addLibrary(libraryData:LibraryData):void
  {
    for each (var functionData:FunctionData in libraryData.functions)
    {
      addFunction(functionData);
    }
    for each (var classData:ClassData in libraryData.classes)
    {
      addClass(classData);
    }
  }

  public function addFunction(functionData:FunctionData):void
  {
    addLinkable(functionData);
    addLinkable(functionData.returnData);
    for each (var parameterData:FunctionParameterData in functionData.parameters)
    {
      addLinkable(parameterData);
    }
  }

  public function addClass(classData:ClassData):void
  {
    addLinkable(classData);
    for each (var fieldData:FunctionParameterData in classData.fields)
    {
      addLinkable(fieldData);
    }
    for each (var methodData:FunctionData in classData.methods)
    {
      addLinkable(methodData);
    }
  }

  public function addLinkable(linkable:ILinkable):void
  {
    var link:String = linkable.toLink();
    if (this._hash[link] != null)
    {
      throw new ArgumentError("Duplicate link: " + link);
    }
    this._hash[link] = linkable;
  }

  public function resolveLink(link:String):*
  {
    return this._hash[link];
  }
}
}